package com.aoip.telemetry.telemetry.datastorage.save;

import com.aoip.telemetry.telemetry.entity.common.TableDataEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenbai
 * @version 1.0
 * @Description TODO
 * @date 2022/4/7 16:40
 */
@Component
public interface DataSaveTemp {

    List<TableDataEntity> error = new ArrayList<>();

    default List<TableDataEntity> splitDataSave(List<TableDataEntity> data) {
        int size = data.size();
        int end = data.size();
        List<TableDataEntity> temp = data.subList(0, size);
        while (size > 0) {
            int num = save(temp);
            if (num == 0) {
                if (end > 1) {
                    end = end / 2;
                    temp = data.subList(0, end);
                } else {
                    if (temp.get(0) != null) {
                        error.add(temp.get(0));
                        data = data.subList(1, size);
                        temp = data;
                        end = --size;
                    }
                }
            } else {
                temp = data.subList(0, end);
                size = size - num;
                end = size;
            }
            if (temp.size() < 1) {
                break;
            }
        }
        return error;
    }

    int save(List<TableDataEntity> list);
}
